package com.jmu.content.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jmu.constant.MessageConstant;
import com.jmu.content.pojo.Agent;
import com.jmu.content.result.Result;
import com.jmu.content.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("adminAgentController")
@RequestMapping("/admin/agent")
public class AgentController {

    @Autowired
    AgentService agentService;

    @GetMapping
    public Result<List<Agent>> getAll(){
        LambdaQueryWrapper<Agent> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc();
        List<Agent> list = agentService.list(queryWrapper);

        if (list==null || list.size()==0){
            return Result.error(MessageConstant.AGENT_NOT_FOUND);
        }

        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<Agent> getById(@PathVariable Long id){
        Agent agent = agentService.getById(id);
        if (agent == null){
            return Result.error(MessageConstant.AGENT_NOT_FOUND);
        }
        return Result.success(agent);
    }

}

